<?php

require_once( FRAMEWORK_PATH . 'models/profile.php' );
require_once( FRAMEWORK_PATH . 'registry/user.class.php' );

class Notificatons{
	private $registry;
	private $user;
	private $profile;
	
	/**
	 * 
	 * 
	 * @param Registry $registry
	 * @param User $user
	 * @param Profile $profile
	 */
	public function __construct( Registry $registry, User $user, Profile $profile)
	{
		$this->registry = $registry;
		$this->user = $user;
		$this->profile = $profile;
	}
	
	public function getNotificationCount()
	{
		$userid = $this->user->getUserID();
		$profileID = $this->profile->getProfileID();
		$sql = "SELECT profile_id from notifications n WHERE n.user_id = $userid  AND n.profile_id = $profileID AND has_read = 0";			
		$this->registry->getObject('db')->executeQuery( $sql );
		$nums = $this->registry->getObject('db')->numRows();
		
		return $nums;
	}
	
	public function viewNotifications()
	{
		$this->registry->getObject('template')->buildFromTemplates('main/main.tpl.php', 'main/main_menu.tpl.php', 'main/main_content.tpl.php',
			'notifications/view.tpl.php', 'notifications/notificationitem.tpl.php' ,'main/footer.tpl.php');
		
		$userid = $this->user->getUserID();
		$profileID = $this->profile->getProfileID();
		$sql = "SELECT * from notifications n WHERE n.user_id = $userid  AND n.profile_id = $profileID";			
		$this->registry->getObject('db')->executeQuery( $sql );
		$nums = $this->registry->getObject('db')->numRows();		
		$data = array();
		
		if( $nums > 0 )
		{
			while( ($row = $this->registry->getObject('db')->getRows()))
			{
				$row['notification_id'] = $row['id'];
				$row['photo'] = $this->registry->getObject('authenticate')->getUser()->getCurrentProfile()->getPhoto();
				$statusID = $row['notification_id'];
				$string = 'notificationdelete'.$statusID;	
				if ($row['has_read'] == 0)
				{
					$row['backgroundcolor'] = 'background-color: #f2f2f2';
				}
				else
				{
					$row['backgroundcolor'] = '';
				}
							
				$row[$string] = "<a class=\"notificationdelete\" href=\"#\" id='$statusID' title='Delete'>X</a>";			
				$data[] = $row;
			}
		}
						
		$cachedata = $this->registry->getObject('db')->cacheData( $data );
			
		$this->registry->getObject('template')->getPage()->addTag( 'notfications_item', array( 'DATA', $cachedata ) );

		$this->registry->getObject('template')->parseOutput();
		
	}
}

?>
